﻿/**
 *-----------------------------------------
 * @file HtmlWriter.cpp
 * Rodeo Game Engine source file
 * Copyright(c) 2010-2011 Marcin Goryca
 *-----------------------------------------
 */
#include "tools\htmlwriter.h"

using namespace std;
using namespace rodeocore;

namespace rodeotools
{
bool HtmlWriter::writeToHTML(const char* filename, string s)
{	
    create(filename);
    addToPage(s);
    CloseHandle(hfile_);
    return true;
}

/**
 *
 */

bool HtmlWriter::create(const char* filename)
{
    hfile_ = CreateFile(filename, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);

    if(hfile_ == INVALID_HANDLE_VALUE)
    {
        MessageBox(NULL, htmlFileLoadFail, HEADER, errIconOK);
        return false;
    }
    return true;
}

/**
 *
 */

void HtmlWriter::writeHeader()
{
    const char* const doctype = "<!DOCTYPE html>\n<html lang=\"en\">\n";
    const char* const header = "<head>\n\t<title>Rodeo Game Engine::Tools</title>\n</head>\n<body>\n";

    DWORD bytesWrite;

    WriteFile(hfile_, doctype, strlen(doctype), &bytesWrite, NULL);
    WriteFile(hfile_, header, strlen(header), &bytesWrite, NULL);
}

/**
 *
 */

void HtmlWriter::addToPage(string s)
{	
    writeHeader();

    DWORD bytesWrite;
    WriteFile(hfile_, s.c_str(), s.length(), &bytesWrite, NULL);

    writeFooter();
}

/**
 *
 */

void HtmlWriter::writeFooter()
{
    DWORD bytesWrite;
    const char* const end = "\n</body>\n</html>";
    WriteFile(hfile_, end, strlen(end), &bytesWrite, NULL);
}
}